<template>
  <div v-if="showMenu">
    <el-container class="app-container">
      <el-header class="app-header" height="64px">
        <div class="header-content">
          <div class="header-left">
            <el-icon class="header-icon" size="24">
              <DataLine />
            </el-icon>
            <h1 class="app-title">All2API 管理平台</h1>
          </div>
          <div class="header-right">
            <el-dropdown>
              <span class="user-info">
                <el-icon><User /></el-icon>
                管理员
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>个人设置</el-dropdown-item>
                  <el-dropdown-item divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside class="app-aside" width="240px">
          <el-menu 
            :default-active="activeIndex" 
            class="sidebar-menu" 
            router
            :collapse="false"
          >
            <el-menu-item index="/dashboard">
              <el-icon><Odometer /></el-icon>
              <span>仪表盘</span>
            </el-menu-item>
            <el-menu-item index="/app/pageList">
              <el-icon><Grid /></el-icon>
              <span>应用管理</span>
            </el-menu-item>
            <el-menu-item index="/api/pageList">
              <el-icon><Connection /></el-icon>
              <span>API管理</span>
            </el-menu-item>
            <el-menu-item index="/dataset/pageList">
              <el-icon><Document /></el-icon>
              <span>数据集管理</span>
            </el-menu-item>
            <el-menu-item index="/datasource/pageList">
              <el-icon><DataBoard /></el-icon>
              <span>数据源管理</span>
            </el-menu-item>
            <el-menu-item index="/namespace/pageList">
              <el-icon><FolderOpened /></el-icon>
              <span>命名空间</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main class="app-main">
          <div class="main-content">
            <router-view></router-view>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import {useRoute} from "vue-router";
import {onMounted, ref, watch} from "vue";
import {
  DataLine,
  User,
  ArrowDown,
  Odometer,
  Grid,
  Connection,
  Document,
  DataBoard,
  FolderOpened
} from '@element-plus/icons-vue';

const route = useRoute();
const activeIndex = ref(route.path);
const showMenu = ref(true);

// 监听路由变化并更新激活菜单项
watch(route, (to) => {
  activeIndex.value = to.path;
});


// 初始化 activeIndex 在组件挂载后
onMounted(() => {
  activeIndex.value = route.path;
});

</script>

<style scoped>
.app-container {
  height: 100vh;
  font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  border-bottom: none;
  padding: 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 24px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-icon {
  margin-right: 12px;
  color: white;
}

.app-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  color: white;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  color: white;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.user-info .el-icon {
  margin-right: 8px;
}

.app-aside {
  background-color: #f8f9fa;
  border-right: 1px solid #e9ecef;
  box-shadow: 2px 0 8px rgba(0,0,0,0.05);
}

.sidebar-menu {
  border-right: none;
  background-color: transparent;
  padding: 16px 0;
}

.sidebar-menu .el-menu-item {
  margin: 4px 16px;
  border-radius: 8px;
  height: 48px;
  line-height: 48px;
  color: #495057;
  font-weight: 500;
  transition: all 0.3s ease;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #e3f2fd;
  color: #1976d2;
}

.sidebar-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.sidebar-menu .el-menu-item .el-icon {
  margin-right: 8px;
  font-size: 18px;
}

.app-main {
  background-color: #f5f7fa;
  padding: 0;
}

.main-content {
  padding: 24px;
  min-height: calc(100vh - 64px);
  background-color: #f5f7fa;
}
</style>